﻿/**
 * Created by Administrator on 2017/8/29.
 */
define(['jquery','InputContact','api','reg'],function ($,InputContact,api,reg) {
    var ContactAdd = {
        name: 'contact-add',
        template: ''
        + '<div class="cnntItem">'
        + '    <div class="addContact cnntBox clearfix">'
        + '        <div class="connection_girl" v-show="contact.Gender===1"></div>'
        + '        <div class="connection_boy" v-show="contact.Gender===0"></div>'
        + '        <div class="right-conent">'
        + '            <div class="right-connection-top">'
        + '                <span style="float:left;"><em>姓　名：</em><input type="text" v-on:input="showGenderOption" v-on:propertychange="showGenderOption" v-model="contact.NameText" />'
        + '                <h3 class="c-shuru" v-show="showGender">'
        + '                    <em v-on:click="isMale">{{maleText}}</em>'
        + '                    <em v-on:click="isFemale">{{femaleText}}</em>'
        + '                </h3></span>'
        + '                <span class="relationTitle">关系：</span>'
        + '                 <p class="model-right">'
        + '                    <i-select v-model="contact.Relation">'
        + '                        <i-option v-for="item in relationTypeOption" v-bind:value="item.value" v-bind:key="item.label">'
        + '                            {{item.label }}'
        + '                        </i-option>'
        + '                    </i-select>'
        + '                 </p>'
        + '            </div>'
        + '            <div class="right-connection-btm">'
        + '                <div class=" cnnt_xl cnnt_sj_xl addPhone">'
        + '                    <input-contact v-model="mobile" v-bind:contactType="defaultType" v-bind:contactTypeText="defaultTypeText" ></input-contact>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_zj_xl">'
        + '                    <span><i></i>座机：<input type="text" v-model="telephone"/></span>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_wx_xl">'
        + '                    <span><i></i>微信：<input type="text" v-model="wechat"/></span>'
        + '                </div>'
        + '                <div class="cnnt_xl cnnt_qq_xl">'
        + '                    <span><i></i>Q Q：<input type="text" v-model="qq"/></span>'
        + '                </div>'
        + '            </div>'
        + '        </div>'
        + '    </div>'
        + '</div>',
        props: {
            customerId: String,
        },
        data: function () {
            return {
                defaultType: 0,
                defaultTypeText: "手机",
                contact: {
                    CustomerId:"",
                    Relation: 0,
                    Name: "",
                    NameText: "",
                    Gender: 0,
                    Contacts: []
                },
                mobile: [{
                    Type: 0,
                    TypeText: "手机",
                    IsUsually: true,
                    ContactValue: ""
                }],
                telephone: "",
                wechat: "",
                qq:"",
                showGender: false,
                /*联系方式类型*/
                relationTypeOption: [
                    {
                        value: 0,
                        label: '父母'
                    },
                    {
                        value: 1,
                        label: '子女'
                    },
                    {
                        value: 2,
                        label: '朋友'
                    },
                    {
                        value: 3,
                        label: '伴侣'
                    },
                    {
                        value: 4,
                        label: '其他'
                    },

                ],
            };
        },
        components: {
            InputContact: InputContact,
        },
        computed: {
            femaleText: function () {
                return $.trim(this.contact.NameText) + "女士";
            },
            maleText: function () {
                return $.trim(this.contact.NameText) + "先生";
            },
        },
        mounted: function () {
            var thisAdd = this;
            //监听submitAddContact
            window.bus.$on("submitAddContact", function () {
                thisAdd.submitAddContact();
            });
        },
        methods: {
            initializeData: function () {
                this.contact = {
                    CustomerId: "",
                    Relation: 0,
                    Name: "",
                    NameText: "",
                    Gender: 0,
                    Contacts: []
                };
            },
            showGenderOption: function () {
                if ($.trim(this.contact.NameText) != "") {
                    this.showGender = true;
                }
            },
            isMale: function () {
                if ($.trim(this.contact.NameText) != "") {
                    this.contact.Gender = 0;
                    this.contact.Name = $.trim(this.contact.NameText);
                    this.contact.NameText = this.maleText;
                }
                else {
                    this.contact.Name = "";
                    this.contact.NameText = "";
                }
                this.showGender = false;
            },
            isFemale: function () {
                if ($.trim(this.contact.NameText) != "") {
                    this.contact.Gender = 1;
                    this.contact.Name = $.trim(this.contact.NameText);
                    this.contact.NameText = this.femaleText;
                }
                else {
                    this.contact.Name = "";
                    this.contact.NameText = "";
                }
                this.showGender = false;
            },
            submitAddContact: function () {
                var thisAdd = this;
                if (thisAdd.contact.Name === "") {
                    this.$Message.error("姓名不能为空");
                    return;
                }
                var c = 0;
                thisAdd.contact.CustomerId = thisAdd.customerId;
                thisAdd.contact.Contacts = [];
                for (var i = 0; i < thisAdd.mobile.length; i++) {
                    if (thisAdd.mobile[i].ContactValue != "") {
                        if (reg.checkPhone(thisAdd.mobile[i].ContactValue)) {
                            this.$Message.error("手机号码错误");
                            return;
                        }
                        thisAdd.contact.Contacts.push({
                            Type: 0,
                            TypeText: "手机",
                            ContactValue: thisAdd.mobile[i].ContactValue,
                            IsUsually: thisAdd.mobile[i].IsUsually,
                        });
                        c++;
                    }
                }
                if ($.trim(thisAdd.telephone) != ""){
                    thisAdd.contact.Contacts.push({
                        Type: 1,
                        TypeText: "座机",
                        ContactValue: $.trim(thisAdd.telephone),
                        IsUsually: true,
                    });
                    c++;
                }
                if ($.trim(thisAdd.wechat) != "") {
                    thisAdd.contact.Contacts.push({
                        Type: 2,
                        TypeText: "微信",
                        ContactValue: $.trim(thisAdd.wechat),
                        IsUsually: true,
                    });
                    c++;
                }
                if ($.trim(thisAdd.qq) != "") {
                    thisAdd.contact.Contacts.push({
                        Type: 3,
                        TypeText: "qq",
                        ContactValue: $.trim(thisAdd.qq),
                        IsUsually: true,
                    });
                    c++;
                }
                if (c === 0) {
                    this.$Message.error("至少输入一个联系方式");
                    return;
                }
                //触发cancelAddOrEdit
                window.bus.$emit("cancelAddOrEdit");
                api.Post_Add_Relative_Contact(thisAdd.contact)
                    .done(function (data) {
                        if (data.IsSuccess) {
                            thisAdd.$Message.success('保存成功');
                            thisAdd.initializeData();
                            //触发refreshContact
                            window.bus.$emit("refreshContact");
                        } else {
                            thisAdd.$Message.error(data.ErrMsg);
                        }
                    })
                    .fail(function () {
                        thisAdd.$Message.error('保存失败');
                    })
            }
        }
    };
    return ContactAdd;
});
